﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AdministratorClient.ServiceReference1;

namespace AdministratorClient
{
    public partial class AdminWindow : Form
    {
        private AdminServiceClient adminService = new AdminServiceClient();

        public AdminWindow()
        {
            InitializeComponent();
            LoadDestinations();
            LoadPlanetypes();
            LoadRoute();
            depaturepicker.Format = DateTimePickerFormat.Custom;
            depaturepicker.CustomFormat = "MM/dd/yyyy hh:mm";
        }
        private void LoadRoute()
        {
            List<WCFRoute> routelist = adminService.FindAllRoutes();
            foreach (WCFRoute item in routelist)
            {
                RouteCombobox.Items.Add(item.RouteId + ". " + item.FromDestination + " -> " + item.ToDestination);
            }
        }
        private void LoadPlanetypes()
        {
            List<WCFPlaneInterior> planeinteriorlist = adminService.FindAllPlaneInteriors();
            foreach (WCFPlaneInterior item in planeinteriorlist)
            {
                PlaneTypeCombobox.Items.Add(item.InteriorId);
            }
        }
        private void LoadDestinations()
        {
            List<WCFDestination> destlist = adminService.FindAllDestinations();
            foreach (WCFDestination item in destlist)
            {
                fromDestsCombo.Items.Add(item.Name);
            }
            foreach (WCFDestination item in destlist)
            {
                toDestsCombo.Items.Add(item.Name);
            }

        }

        private void AddDestinationToComboboxes(string name)
        {
            fromDestsCombo.Items.Add(adminService.FindDestinationName(name).Name);
            toDestsCombo.Items.Add(adminService.FindDestinationName(name).Name);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name;
            string country;
            if (destNameBox.Text != null)
            {
                name = destNameBox.Text;

                if (destCountryBox.Text != null)
                {
                    country = destCountryBox.Text;
                    if (adminService.AddDestination(name, country))
                    {
                        destDoneLabel.Text = "Nice job";
                        destCountryBox.Clear();
                        destNameBox.Clear();
                        AddDestinationToComboboxes(name);
                    }
                    
                }

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string from;
            string to;
            string price;
            string time;

            if (fromDestsCombo.SelectedIndex == toDestsCombo.SelectedIndex)
            {
                routeDoneLabel.Text = "Næh";
            }
            else if (adminService.FindRoute(fromDestsCombo.Text, toDestsCombo.Text) != null)
            {

                routeDoneLabel.Text = "Nope";
            }
            else if (fromDestsCombo.Text != null)
            {
                from = fromDestsCombo.Text;
                if (toDestsCombo.Text != null)
                {
                    to = toDestsCombo.Text;
                    if (priceBox != null)
                    {
                        price = priceBox.Text;
                        if (timeBox != null)
                        {
                            time = timeBox.Text;
                            if (adminService.AddRoute(from, to, price, time))
                            {
                                routeDoneLabel.Text = "Yeah buddy";
                            }

                        }
                    }
                }

            }
        }

        private void AddFlightButton_Click(object sender, EventArgs e)
        {
            int route = RouteCombobox.SelectedIndex;
            if (route != -1)
            {
                if (adminService.AddPlane(PlaneTypeCombobox.Text, gatetextbox.Text, depaturepicker.Value, route))
                {
                    Addflightdone.Text = "good job";
                }
                else
                {
                    Addflightdone.Text = "fight me";
                }
            }
            else
                Addflightdone.Text = "scrub";
        }

        private void AddInteriorButton_Click(object sender, EventArgs e)
        {
            if (adminService.AddPlaneInterior(ModelTextbox.Text, FirstClassTextbox.Text, NormalseatsTextbox.Text))
            {
                AddInteriorLable.Text = "Nice";
            }
            else
            {
                AddInteriorLable.Text = "Shit";
            }
            
        }
    }
}
